const App = getApp()
var utils = require('../../utils/classDict');
const computedBehavior = require('miniprogram-computed').behavior

Page({
  behaviors: [computedBehavior],
  data: {
    studentList: [],
    addstatus: false,
    classDict: utils.classDict,
    clsList: [],
    form: {
      userName: '', //学生姓名
      gradeLevel: 0, //年级1~9
      className: 0, //班级
      gender: "", //性别
      schoolName: "", //学校
      soundCourseType: "", // 声部类型
      locationId: ""
    },
    soundCourseType: [],
    soundIndex: '',
    locationId: "",
    bindId: '',
    locationList: []
  },
  computed: {
    className(data) {
      let name = utils.classDict[0][data.clsList[0]]?.name
      return name
    },
    classIndex(data) {
      let name = utils.classDict[1][data.clsList[1]]?.name
      return name
    },
  },
  onLoad(options) {
    if (options) {
      this.setData({
        bindId: options.bindId
      })
    }
  },
  onReady() {},
  async onShow() {
    await this.getStudentList()
    if (!this.data.soundCourseType.length) {
      await this.getDic()
    }
    if (!this.data.locationList.length) {
      await this.getLocation()
    }
  },
  async getStudentList() {
    // 学生查询
    // let name = App.globalData.userInfo.user.nickName
    let data = await App.$axios.request({
      url: `/parent/myCurriculum/student`,
      method: 'GET',
      loading: true,
      data: {
        // name: "皮卡卡"
      },
    })
    if (data) {
      data.forEach(e => {
        e.class = `${e.gradeLevel?e.gradeLevel+'年级':''}${e.className?e.className+'班':''}`
      })
      this.setData({
        studentList: data
      })
    }
  },
  async addStudent() {
    this.setData({
      addstatus: true
    })
  },
  classChange(item) {
    // 班级修改
    let cls = JSON.parse(JSON.stringify(item.detail.value))
    let classDict = this.data.classDict
    this.setData({
      'form.gradeLevel': classDict[0][cls[0]].id,
      'form.className': classDict[1][cls[1]].id,
      clsList: [cls[0], cls[1]]
    })
  },
  typeChange(item) {
    // 声部选择
    let value = this.data.soundCourseType[item.detail.value]
    this.setData({
      soundIndex: item.detail.value,
      'form.soundCourseType': value.dictCode
    })
  },
  locationChange(item) {
    // 上课地点选择
    let value = this.data.locationList[item.detail.value]
    this.setData({
      locationId: item.detail.value,
      'form.locationId': value.dictCode
    })
  },
  sexChange(item) {
    // 性别修改
    this.setData({
      'form.gender': parseInt(item.detail.value)
    })
  },
  changeInput(item) {
    // 姓名修改
    this.setData({
      'form.userName': item.detail.value
    })
  },
  changeSchool(item) {
    // 学校修改
    this.setData({
      'form.schoolName': item.detail.value
    })
  },
  async addBoxClick(e) {
    //  新增学生弹窗点击
    let form = this.data.form
    if (e.currentTarget.dataset.type == 'ok') {
      if (!form.userName) {
        wx.showToast({
          title: '学生姓名不可为空',
          icon: 'none'
        })
        return
      }
      if (isNaN(parseInt(form.gender))) {
        wx.showToast({
          title: '学生性别不可为空',
          icon: 'none'
        })
        return
      }
      //todo 临时解决
      form.gradeLevel= 1;
      form.className= 1;
      if (!form.gradeLevel || !form.className) {
        wx.showToast({
          title: '年级、班级信息不可为空',
          icon: 'none'
        })
        return
      }
      form.id = this.data.bindId
      let data = await App.$axios.request({
        url: '/parent/myCurriculum/vipPerfect',
        method: 'PUT',
        loading: true,
        data: form
      })
      console.log(data)
      if (data) {
        wx.switchTab({
          url: '/pages/course/course'
        })
        setTimeout(() => {
          wx.showToast({
            title: '操作成功',
            icon: 'none'
          })
        }, 1000)
      } else {
        return
      }
    }
    this.setData({
      addstatus: false,
      clsList: [],
      form: {
        userName: '', //学生姓名
        gradeLevel: 0, //年级1~9
        className: 0, //班级
        gender: "", //性别
        schoolName: "", //学校
        soundCourseType: ""
      },
      soundIndex: ""
    })
  },
  async getDic() {
    // 获取字典
    let data = await App.$axios.request({
      url: `/system/dict/data/type/sound_course_type`,
      method: 'GET',
      loading: true,
    })
    if (data) {
      let ls = []
      data.forEach(e => {
        ls.push({
          dictLabel: e.dictLabel,
          dictCode: e.dictValue
        })
      })
      this.setData({
        soundCourseType: ls
      })
    }
  },
  async getLocation() {
    // 获取教学地点
    let data = await App.$axios.request({
      url: `/parent/myCurriculum/location`,
      method: 'GET',
      loading: true,
    })
    if (data) {
      let ls = []
      data.forEach(e => {
        ls.push({
          dictLabel: e.locationName,
          dictCode: e.id
        })
      })
      this.setData({
        locationList: ls
      })
    }
  },
  async chooseStudent(e) {
    if (this.data.addstatus) return
    let item = e.currentTarget.dataset.item
    this.setData({
      form: {
        userid: item.id,
        userName: item.userName, //学生姓名
        gradeLevel: item.gradeLevel - 1, //年级1~9
        className: item.className - 1, //班级
        gender: item.gender, //性别
        schoolName: item.schoolName, //学校
        soundCourseType: '' // 声部类型
      },
      soundIndex: "",
      clsList: [item.gradeLevel - 1, item.className - 1],
      addstatus: true
    })
  }
})
